package com.yoso.shop.interfaces.controller.stock;

import com.alibaba.fastjson.JSON;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.BaseEntityExample;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.service.IStockService;
import com.yoso.shop.domain.stock.StockEntity;
import com.yoso.shop.infrastructure.constants.stock.StockConstants;
import com.yoso.shop.interfaces.vo.stock.StockVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shizejin
 * @date 2022/1/30
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("/stock")
@Api(tags = "库存")
@Slf4j
public class StockController extends BaseController<StockVo, StockEntity> {
    @Autowired
    private IStockService stockService;

    @Override
    public StockEntity V2E(StockVo data) {
        return JSON.parseObject(JSON.toJSONString(data), StockEntity.class);
    }

    @Override
    public StockVo E2V(StockEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), StockVo.class);

    }

    @Override
    public IBaseService<StockVo, ? extends BaseEntityExample> getService() {
        return this.stockService;
    }

    @ApiOperation("获得未维护库存的商品")
    @PostMapping("getUnSafeguardStock")
    public BaseResponse getMasterBranch() {
        return this.success("操作成功", stockService.getUnSafeguardStock());
    }

    @Override
    public BaseResponse updateById(@RequestBody StockVo vo) {
        this.stockService.addStock(vo, StockConstants.SOURCE_ADMIN_ADD, StockConstants.STOCK_ADD);
        return this.success();
    }
}
